'use strict';

const {
	Controller
} = require('egg');

class UserController extends Controller {

  async login() {
    const { ctx } = this;
    console.log('访问了')
    ctx.body = '访问了登录路由';
  }
  async select() {
    const { ctx } = this;
    // 查询出所有的用户并返回
    const results = await this.app.mysql.query(`select uid,nickname,tel,gender,type from user`)
    ctx.body = {
      code: 1,
      data:results
    };
  }
  async select_id() {
    const { ctx } = this;
    let uid = ctx.request.query.uid
    console.log(uid,'===')
    // 查询出指定用户并返回
    const results = await this.app.mysql.query(`select uid,nickname,tel,gender,type from user where uid=${uid}`)
    ctx.body = {
      code: 1,
      data:results
    };
  }
  async select_nickname() {
    const { ctx } = this;
    let nickname = ctx.request.query.nickname
    console.log(nickname)
    // 根据用户名查询出指定用户并返回
    const results = await this.app.mysql.query(`select uid,nickname,tel,gender,type from user where nickname like '%${nickname}%'`)
    console.log(results)
    ctx.body = {
      code: 1,
      data:results
    };
  }
  async select_sex() {
    const { ctx } = this;
    let gender = ctx.request.query.sex
    console.log(gender)
    // 根据用户名查询出指定用户并返回
    const results = await this.app.mysql.query(`select uid,nickname,tel,gender,type from user where gender='${gender}'`)
    console.log(results)
    ctx.body = {
      code: 1,
      data:results
    };
  }
  async select_type() {
    const { ctx } = this;
    let type = ctx.request.query.type
    console.log(type)
    // 根据用户名查询出指定用户并返回
    const results = await this.app.mysql.query(`select uid,nickname,tel,gender,type from user where type=${type}`)
    console.log(results)
    ctx.body = {
      code: 1,
      data:results
    };
  }
  async adduser() {
    const { ctx } = this;
    let {nickname,tel,pwd,sex,type} = ctx.request.body
    console.log(ctx.request.body)
    const results1 = await this.app.mysql.query(`select * from user where tel='${tel}'`)
    if(results1.length != 0){
      console.log(results1)
      ctx.body = {
        code: -1,
        data:'用户已存在'
      };
    }else {
      const results = await this.app.mysql.query(`insert into user(nickname,tel,pwd,gender,touxiang,type) values('${nickname}','${tel}','${pwd}','${sex}','/public/img/toux.webp',${type})`)
      console.log(results)
      ctx.body = {
        code: 1,
      };
    }    
  }
  async deleteuser() {
    const { ctx } = this;
    let uid = ctx.request.query.uid
    console.log(uid)
    const results = await this.app.mysql.query(`select * from user where tel='${tel}'`)
    if(!results){
      ctx.body = {
        code: -1,
        data: '该用户已经不存在'
      }; 
    }else{
      const results = await this.app.mysql.query(`DELETE FROM user WHERE uid = ${uid}`)
      // console.log(results)
      ctx.body = {
        code: 1,
        data: '删除成功'
      };  
    }
  }
	// 获取用户信息部分
	async getuserinfo() {
		// 前端传递过来的数据：count,keyword,uid
		const { ctx } = this
		// let resp = this.ctx.request.query
		// console.log(1111111111, resp)
		let usersql = `select nickname,tel,touxiang,type,gender,count(concerned_id) guanzhu,introduction from user,concern where user.uid=concerned_id and user.uid=1`
		let result = await this.ctx.app.mysql.query(usersql)
		let obj = {
			nickname: result[0].nickname,
			touxiang: result[0].touxiang,
			tel: result[0].tel,
			gender: result[0].gender,
			type: result[0].type,
			guanzhu: result[0].guanzhu,
			introduction: result[0].introduction,
			created_at: result[0].created_at,
			updated_at: result[0].updated_at,

		}
		ctx.body = obj


	}
	// 修改用户信息部分
	async setting() {
		const { ctx } = this
		let files = ctx.request.files
		let { unickname, utouxiang, ugender, utype, uintro, upwd } = ctx.request.query;
		let userid = 3
		console.log("前端传递过来的值", files);
		console.log("昵称", ctx.request.query);
		let sql = `update user set touxiang='${utouxiang} ', nickname='${unickname}',  gender='${ugender}',  type='${utype}', 
		 introduction='${uintro}',  pwd='${upwd}'where uid =${userid} `;
		console.log(sql);
		let result = await this.ctx.app.mysql.query(sql);
		if (result.affectedRows === 1) {
			ctx.body = {
				code: 24,
				msg: result,
			};
		}



	}
	// 创建画板部分
	async addraw() {
		const { ctx } = this
		let { drawname, DTYPE } = ctx.request.query;
		console.log(drawname, "==========================");
		let usersql = `insert into draw_board (bname,btype,uid) values ('${drawname}','${DTYPE}','2')`
		let result = await this.ctx.app.mysql.query(usersql)
		ctx.body = result


	}



}

module.exports = UserController;